/*
 * VisualClassComponentVia.cpp
 *
 *  Created on: May 29, 2013
 *      Author: root
 */

#include "gui/VisualComponents/clases/VisualClassComponentVia.h"
#include "gui/VisualComponents/VisualComponentConector.h"
#include "gui/VisualComponents/VisualComponentSegment.h"
//#include "gui/SubWindow/ViewModifyClassViaComponentInfoWin.h"
#include "model/Utils.h"
#include "model/der/Fork.h"
#include "gui/SubWindow/ViewModifyComponentInfoWin.h"
#include "gui/VisualComponents/ArrowStyleCreator.h"

VisualClassComponentVia::VisualClassComponentVia(Diagram* diagram) : VisualDiagramComponent(diagram), VisualComponentVia(diagram), VisualClassesComponent(diagram) {
	m_component = new ViaClasses();
	setStartConector(NULL);
	setEndConector(NULL);
	initialize();
}

VisualClassComponentVia::VisualClassComponentVia(VisualComponentConector* start, Diagram* diagram, VisualComponentConector* end) :
		VisualDiagramComponent(diagram), VisualComponentVia(start, diagram, end), VisualClassesComponent(diagram) {
	m_component = new ViaClasses();
	setStartConector(start);
	setEndConector(end);
	m_component->setX(m_startConector->getX());
	m_component->setY(m_startConector->getY());
	createSegments();
	initialize();
}

VisualClassComponentVia::VisualClassComponentVia(const VisualClassComponentVia& other) :
			VisualDiagramComponent(other.m_diagram), VisualComponentVia(other.m_diagram), VisualClassesComponent(other.m_diagram) {
	m_component = new ViaClasses(*dynamic_cast<ViaClasses*>(other.m_component));
	initialize();
	initializeSegments();
}

VisualClassComponentVia::~VisualClassComponentVia() {

}

void VisualClassComponentVia::alignPoints( Point* newPoint, Point* alignPoint, int x, int y, bool allignY){
//	y += 20;
	if (newPoint->getY() == alignPoint->getY() && allignY)
		alignPoint->setY(y);
	newPoint->setX(x);
	newPoint->setY(y);
}

void VisualClassComponentVia::showProperties(){
}

void VisualClassComponentVia::load(XmlReader& reader, Diagram* diagram){
	VisualClassesComponent::load(reader, reader, diagram);
	this->initializeSegments();
}

void VisualClassComponentVia::load(XmlReader& reader, XmlReader& readerModel, Diagram* diagram) {
	VisualClassesComponent::load(reader, readerModel, diagram);
	ViaClasses* viaClases = dynamic_cast<ViaClasses*>(getComponent());
	if (viaClases != NULL) {
		ArrowStyleCreator::setArrowStyle(buildArrowSty(viaClases->tipoDeRelacionEntreClases));
	}
	this->initializeSegments();
}

VisualClassesComponent* VisualClassComponentVia::getCopy(Diagram* diagram){
	Diagram* origen = m_diagram;
	m_diagram = diagram;
	VisualClassesComponent* comp = new VisualClassComponentVia(*this);
	m_diagram = origen;
	return comp;
}

void VisualClassComponentVia::draw(Cairo::RefPtr<Cairo::Context> ptrContext){

	ViaClasses* via = dynamic_cast<ViaClasses*>(m_component);
	ptrContext->save();

	VisualDiagramComponent::draw( ptrContext);
	Draw draw;
	std::list< Point* >::iterator it = via->getPoints().begin();
	Point* point1 = (*++it);
	Point* point2 = (*++it);
	/*draw.writeTextInContext(ptrContext,  (point1->getX() + point2->getX() ) / 2 + 14,
			( point1->getY() + point2->getY() ) / 2 - 2 , via->stringParaTipoDeRelacionEntreClases(via->tipoDeRelacionEntreClases), COLOR_IN_TEXT);*/
	if(via->getName() != "rol")
		draw.writeTextInContext(ptrContext,  (point1->getX() + point2->getX() ) / 2 + 18,
				( point1->getY() + point2->getY() ) / 2 + 12 , via->getName(), COLOR_IN_TEXT);
}

